<?php

class Core_Backend_Component_Design_Controller_Page_Template extends Dcmp_Component_Controller {

    public function indexAction($integer_pageindex = 1, $integer_pagesize = 20, $sort = 'id', $dir = 'desc') {
        $this->getHistory()->record();
        $validator = $this->getRequest()->getValidator('filter');
        $collection = App::getModel('design/collection_page_template')
                        ->joinPageTheme()
                        ->joinWebsiteTheme()
                        ->filterByAreaId($this->getSession()->get('managed_area_id'))
                        ->pagination($integer_pageindex, $integer_pagesize)
                        ->load();
        $records = $collection->loadCount();
        return $this->viewResult(array('list' => $collection, 'records' => $records));
    }

    public function createAction() {
        $pageThemes = App::getModel('design/collection_page_theme')
                        ->filterByAreaId($this->getSession()->get('managed_area_id'))
                        ->filterByEnabled(1)
                        ->load();
        $websiteThemes = App::getModel('design/collection_website_theme')
                        ->filterByAreaId($this->getSession()->get('managed_area_id'))
                        ->filterByEnabled(1)
                        ->load();
        return $this->viewResult(array('page_themes' => $pageThemes, 'website_themes' => $websiteThemes));
    }

    public function editAction($id) {
        $model = App::getModel('design/page_template')->load($id);
        $area = App::getModel('global/area')->load($this->getSession()->get('managed_area_id'));
        $this->getPage()->getNavigation()->addLabel($model->getName());
        $model->setTemplateScript(Kd_Util_Directory::readFile(
                        DP_DESIGN .
                        $area->getNamespace() . DS .
                        $model->getWebsiteTheme()->getName() . DS .
                        'template' . DS .
                        $model->getPageTheme()->getType() . DS .
                        $model->getPageTheme()->getName() . DS .
                        str_replace('_', DS, $model->getFileName()) . '.phtml'));
        return $this->createAction()->setModel($model);
    }

    public function applyPostAction() {
        return $this->saveModel(FALSE);
    }

    public function savePostAction() {
        return $this->saveModel();
    }

    private function saveModel($return = TRUE) {
        $data = $this->getRequest()->getForm('template');
        $model = App::getModel('design/page_template');
        if (isset($data['id']) && !empty($data['id'])) {
            $model->load($data['id']);
        }
        $model->addData($data);
        if (($errors = $model->validate()) === TRUE) {
            $model->save();
            $area = App::getModel('global/area')->load($data['area_id']);
            Kd_Util_Directory::writeFile($this->getRequest()->getForm('template_script'),
                            DP_DESIGN . 
                            $area->getNamespace() . DS .
                            $model->getWebsiteTheme()->getName() . DS .
                            'template' . DS .
                            $model->getPageTheme()->getType() . DS .
                            $model->getPageTheme()->getName() . DS .
                            str_replace('_', DS, $model->getFileName()) . '.phtml');

            $this->getMessager()->addInform($this->__('share.message.success'));
            return $return ?
                    $this->redirectResult($this->getHistoryUrl('design/page_template/index')) :
                    $this->redirectResult($this->getUrl(array('id' => $model->getIdValue()), 'edit'));
        } else {
            $this->getMessager()
                    ->addError($this->__('share.message.fail'))
                    ->addError($errors);
            return $this->viewResult(array('model' => $model));
        }
    }

}